package com.ht.activiti.config;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.HashSet;
import java.util.Set;

/**
 * 这个是 shiro  框架也不用管
 * @author zhangfc
 * @date 2021/11/3 13:53
 */
public class CustomRealm extends AuthorizingRealm {
    //此方法为权限设置的方法.不同的用户登录所看到的界面都是不同的.
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName = (String) SecurityUtils.getSubject().getPrincipal();
        System.out.println(userName);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        Set<String> stringSet = new HashSet<>();
        stringSet.add("user:show");
        stringSet.add("user:admin");
        stringSet.add("user:list");
        simpleAuthorizationInfo.setStringPermissions(stringSet);
        return simpleAuthorizationInfo;
    }

    /**
     * 这里可以注入userService 这里写死帐号和密吗
     * <p>
     * 获取需要认证的信息
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("---------身份认证方法");
        String userName = (String) authenticationToken.getPrincipal();
        String userPwd = new String((char[]) authenticationToken.getCredentials());
        //根据用户名从数据库获取密码
        String passWord = "123";
        if (userName == null) {
            throw new AccountException("用户名不正确");
        } else if (!passWord.equals(userPwd)) {
            throw new AccountException("密码不正确");
        }
        return new SimpleAuthenticationInfo(userName, passWord,  getName());
    }
}
